/**
 * Created by Administrator on 2017/2/8.
 */

$(function () {
    var key = getCookie('key');
    var store_id = getCookie('store_id');
    // if (store_id) {
    //     window.location.href = WapSiteUrl + '/tmpl/member/my_store.html';
    // }
    if (!key) {
        window.location.href = WapSiteUrl + '/tmpl/member/login.html';
    }
        $.ajax({
            type:'post',
            url:ApiUrl+"/index.php?act=store&op=my_store",
            data:{key:key},
            dataType:'json',
            success: function (result) {
                if (result.code == 400) {
                    $.sDialog({
                        skin: "red",
                        content: result.datas.error,
                        okBtn: true,
                        cancelBtn: true,
                        "okBtnText": "开店", //确定按钮的文字
                        "cancelBtnText": "取消", //取消按钮的文字
                        "okFn": function () {
                            window.location.href = WapSiteUrl + '/tmpl/member/my_shop_taocan.html';
                        }, //点击确定按钮执行的函数
                        "cancelFn": function () {
                            window.location.href = WapSiteUrl + '/tmpl/member/member.html';
                        } //点击取消按钮执行的函数
                    });
                    return false;
                }
                get_footer();
                if (!result.datas.open_store) {
                    window.location.href = WapSiteUrl + '/tmpl/member/my_shop_taocan.html';
                }
            }
        });

    $.sValid.init({
        rules:{
            store_name:"required",
            letter:"required"
        },
        messages:{
            store_name:"店铺名称必须填写！",
            letter:"店铺缩写必须填写!"
        },
        callback:function (eId,eMsg,eRules){
            if(eId.length >0){
                var errorHtml = "";
                $.map(eMsg,function (idx,item){
                    errorHtml += "<p>"+idx+"</p>";
                });
                errorTipsShow(errorHtml);
            }else{
                errorTipsHide();
            }
        }
    });

    var allow_submit = true;
    $('.footer').click(function(){//会员登陆
        if (allow_submit) {
            allow_submit = false;
        } else {
            return false;
        }
        var store_name = $('#store_name').val();
        var store_letters = $('#letter').val();
        var store_avatar = $('#imgs').val();
        var store_description = $('#description').val();

        var RegTex = /^[A-Za-z]+$/;;
        if(!RegTex.test(store_letters)){
              creatLayout("店铺缩写只能是字母");
              allow_submit = true;
              return;
        }
        if($.sValid()){
            $.ajax({
                type:'post',
                url:ApiUrl+"/index.php?act=store&op=store_setting",
                data:{key: key,store_name:store_name,store_letters:store_letters,store_avatar:store_avatar,store_description:store_description},
                dataType:'json',
                success:function(result){
                    allow_submit = true;
                    $.sDialog({
                        skin:"red",
                        content:result.datas.msg,
                        okBtn:true,
                        "okBtnText": "确定", //确定按钮的文字
                        "okFn": function() { window.location.href = WapSiteUrl + '/tmpl/member/my_store.html';}, //点击确定按钮执行的函数
                    });
                }
            });
        } else {
            allow_submit = true;
            if (store_name == '') {
                $.sDialog({
                    skin:"red",
                    content:'店铺名称必须填写',
                    okBtn:true,
                    "okBtnText": "确定", //确定按钮的文字
                });
                return;
            }
            if (store_letters == '') {
                $.sDialog({
                    skin:"red",
                    content:'名称首字母缩写必须填写',
                    okBtn:true,
                    "okBtnText": "确定", //确定按钮的文字
                });
                return;
            }
        }
    });

//初始化页面
//     initPage();
    get_footer();

});
function get_footer() {
    if (!footer) {
        footer = true;
        $.ajax({
            url: WapSiteUrl + '/js/tmpl/footer.js',
            dataType: "script"
        });
    }
}

function upload(obj) {
    var _self = obj;
    var _this = $(obj);
    var fileReader = new FileReader();
    fileReader.onload = function (e) {
        if (fileReader.readyState == FileReader.DONE) {
            var file = _self.files[0];
            var size = file.size;
            var width = file.width;
            var height = file.height;
            var param_num = _this.attr('num');//上传图片数量
            var param_count = _this.attr('count');//已上传图片数量
            $("#avatar").attr('src',this.result);
            $("#imgs").val(this.result);
        }
    };
    //一次加载多个文件
    var i = 0, src = _self.files;
    fileReader.readAsDataURL(src[i]);
    fileReader.onloadend = function () {
        i++;
        if (i < src.length) fileReader.readAsDataURL(src[i]);
    }
}

function uploadBanner(obj) {
    var _self = obj;
    var _this = $(obj);
    var fileReader = new FileReader();
    fileReader.onload = function (e) {
        if (fileReader.readyState == FileReader.DONE) {
            var file = _self.files[0];
            var size = file.size;
            var width = file.width;
            var height = file.height;
            var param_num = _this.attr('num');//上传图片数量
            var param_count = _this.attr('count');//已上传图片数量
            $("#avatar1").attr('src',this.result);
            $("#thumbBaner").val(this.result);
        }
    };
    //一次加载多个文件
    var i = 0, src = _self.files;
    fileReader.readAsDataURL(src[i]);
    fileReader.onloadend = function () {
        i++;
        if (i < src.length) fileReader.readAsDataURL(src[i]);
    }
}